import {GTree} from 'geminis-react';
import {forwardRef, Key, useCallback, useImperativeHandle, useRef, useState} from 'react';

export const OrgTree = forwardRef((props: any, ref: any) => {
    const tree = useRef<any>();
    const [selection, setSelection] = useState<any>();

    useImperativeHandle(
        ref,
        () => {
            return {
                selection: tree.current.selection,
                reloadSelectedNode: () => tree.current.reloadSelectedNode(),
            };
        },
        [selection],
    );

    const onSelected = useCallback((selection: any[]) => {
        setSelection(selection);
        props.onSelected?.(selection);
    }, []);

    return (
        <GTree
            ref={tree}
            url="/api/resources/organization/children/"
            root={props.showRoot ? {id: '', name: '组织机构', pathId: '/'} : null}
            {...props}
            onSelected={onSelected}
        />
    );
});
